#include "BlockQueue.hpp"
#include "Task.hpp"
// #include <pthread.h>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>
void *consumer(void *args)
{
    //BlockQueue<int> *bp = static_cast<BlockQueue<int> *>(args);
    BlockQueue<Task> *bp = static_cast<BlockQueue<Task> *>(args);

    while (true)
    {
        sleep(1);
        // 消费数据 bp->pop()
        // int data = 0;
        // bp->Pop(&data);
        Task t;
        bp->Pop(&t);
        t.Run();
        // 进行处理
        //std::cout << "consumer data " << data << std::endl;
        std::cout << "consumer data " << t.printresult() << std::endl;

    }
    return nullptr;
}

void *productor(void *args)
{
    //BlockQueue<int> *bp = static_cast<BlockQueue<int> *>(args); // 生产者和消费者看到同一块资源
    BlockQueue<Task> *bp = static_cast<BlockQueue<Task> *>(args); // 生产者和消费者看到同一块资源

    while (true)
    {
        // 1.有数据
        //int data = rand()%10 + 1;
        int data1 = rand() % 10;
        usleep(rand()%123);
        int data2 = rand() % 10;
        char oper = opers[rand() % opers.size()];
        
        Task t(data1,data2,oper);
        // // 2.生产
        // // bp->push()
        // bp->Push(data);
        // std::cout << "productor data " << data << std::endl;
        //sleep(1);
        bp->Push(t);
        std::string str = t.print();
        std::cout<<"productor data " << str << std::endl;
    }
    return nullptr;
}
int main()
{
    srand((uint16_t)time(nullptr) ^ getpid() ^ pthread_self());
    //BlockQueue<int> *bp = new BlockQueue<int>();
    BlockQueue<Task> *bp = new BlockQueue<Task>();
    pthread_t c, p; // 消费者和生产者
    pthread_create(&c, nullptr, consumer, bp);

    pthread_create(&p, nullptr, productor, bp);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
}